package edu.gatech.volt2.drdoommgt.domain;

import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.GenericGenerator;

/**
 * This class represents an appointment of the hospital
 * 
 * @author Perron Jones
 */

@Entity
@Table(name="appointments")
public class Appointment 
{
	@Id
	@GeneratedValue(generator="increment")
	@GenericGenerator(name="increment", strategy = "increment")
	private long id;				// Unique id of the appointment
	
	private String patientName;
	private String reason;
	@Temporal(TemporalType.TIMESTAMP)
	private Calendar requestedDate;

	@ManyToOne(cascade = CascadeType.PERSIST)
	private Doctor requestedDoc;

	private boolean confirmed;
	
	/**
	 * This default constructor sets the metadata of the appointment to
	 * customized default values.
	 */
	public Appointment()
	{
		this.patientName= "";
		this.reason= "N/A";
		this.requestedDate= Calendar.getInstance(); //if we initialize to NULL, we can not set stuff to it
		this.requestedDoc= null;
		this.confirmed= false;
	}
	
	/**
	 * This constructor sets the metadata of the appointment to the information
	 * passed into it.
	 * 
	 * @param reason the reason for the appointment
	 * @param requestedDate the requested date of the appointment
	 * @param requestedDoc the desired doctor for the appointment
	 */
	public Appointment(String patientName,String reason, Calendar requestedDate, Doctor requestedDoc)
	{
		this();
		this.patientName= patientName;
		this.reason= reason;
		this.requestedDate= requestedDate;
		this.requestedDoc= requestedDoc;

	}
	

	/**
	 * @return the patientName
	 */
	public String getPatientName() {
		return patientName;
	}

	/**
	 * @param patientName the patientName to set
	 */
	public void setPatientName(String patientName) {
		this.patientName = patientName;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(long id) {
		this.id = id;
	}

	/**
	 * @return the id
	 */
	public long getId() {
		return id;
	}

	/**
	 * This method creates a string representation of the appointment.
	 * 
	 * @return a string representation of the appointment
	 */
	public String toString()
	{
		String appointmentToString= "";

		String date= this.requestedDate.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH) +
		" " + Integer.toString(this.requestedDate.get(Calendar.DAY_OF_MONTH)) +
		", " + Integer.toString(this.requestedDate.get(Calendar.YEAR)) +
		" - " + Integer.toString(this.requestedDate.get(Calendar.HOUR)) +
		":" + (this.requestedDate.get(Calendar.MINUTE) < 10 ? "0"+Integer.toString(this.requestedDate.get(Calendar.MINUTE)):Integer.toString(this.requestedDate.get(Calendar.MINUTE)) )+
		" " + this.requestedDate.getDisplayName(Calendar.AM_PM, Calendar.SHORT, Locale.ENGLISH);
		

		appointmentToString= "Patient: " + patientName + "\nReason: " + reason + "\nRequested Date: " + date + "\nRequested Doctor: Dr. " +
		requestedDoc.getName().toString() + "\nConfirmed: " + this.confirmedToString() + "\n";
		
		return appointmentToString;
	}

	/**
	 * 
	 * @param reason the reason for the appointment to set
	 */
	public void setReason(String reason)
	{
		this.reason = reason;
	}

	/**
	 * 
	 * @return the reason for the appointment
	 */
	public String getReason()
	{
		return reason;
	}

	/**
	 * 
	 * @param requestedDate the date to request the appointment to set
	 */
	public void setRequestedDate(Calendar requestedDate) {
		this.requestedDate = requestedDate;
	}

	/**
	 * 
	 * @return the requested date of the appointment
	 */
	public Calendar getRequestedDate()
	{
		return requestedDate;
	}

	/**
	 * 
	 * @param requestedDoc the desired doctor for the appointment to set
	 */
	public void setRequestedDoc(Doctor requestedDoc) 
	{
		this.requestedDoc = requestedDoc;
	}

	/**
	 * 
	 * @return the desired doctor for the appointment
	 */
	public Doctor getRequestedDoc() 
	{
		return requestedDoc;
	}
	




	/**
	 * 
	 * @param confirmed whether or not the appointment is confirmed
	 */
	public void setConfirmed(boolean confirmed)
	{
		this.confirmed= confirmed;
	}
	
	/**
	 * 
	 * @return the confirmation status of the appointment
	 */
	public boolean getConfirmed()
	{
		return confirmed;
	}
	
	/**
	 * This method creates a string representation of the confirmation.
	 * 
	 * @return a string representation of the confirmation
	 */
	public String confirmedToString()
	{
		return (this.confirmed ? "yes": "no");
	}

	public static List<Appointment> getListForUser(User patient) {
		// Return a list of appointments for the user 
		// SELECT * FROM `appointments` WHERE `user_id` = patient.getId;
		return null;
	}
}
